<template>
	<view class="page">
		<u-navbar title="工地列表" border @leftClick="$utils.back()" placeholder>
		</u-navbar>
		<filter @reload='reload' />
		<view style="flex: 1;margin-top: 20rpx;">
			<z-paging ref="paging" style="height: 100%;" :fixed="false" v-model="data" @query="query"
				:default-page-no="1" :default-page-size="10">
				<template v-if="!loading">
					<view class="userInfoBox" v-for="item in data" :key="item.id"
						@click="jumpDetail(item.id,item.avatar)">
						<view style="position: relative;">
							<view v-if="item.is_recommend" class="icon-hot_light iconfont"></view>
							<u--image :fade='false' :src="item.avatar" width="100rpx" height="100rpx"
								radius='8rpx'></u--image>
						</view>
						<view>
							<view class="flex">
								<view class="flex"
									style="height: 100%;justify-content: space-between;flex: 1;flex-direction: column;">
									<u-text v-if='item.remark_name' :lines='1' size='24rpx'
										:text='item.remark_name'></u-text>
									<u-text v-else :lines='1' size='24rpx' text='暂无备注'></u-text>
									<u-text :lines='1' size='24rpx' :text='item.customer.nick_name'></u-text>
								</view>
								<view @click.stop="()=>{}">
									<u-icon name="more-dot-fill" size="30rpx" @click='openSelect(item)'></u-icon>
								</view>
							</view>
						</view>
					</view>
				</template>
				<template v-else>
					<view class="userInfoBox" v-for="item in 20" :key="item">
						<view style="height: 100rpx;width: 100rpx;border-radius: 8rpx;" class="Skeleton" />
						<view>
							<view style="height: 30rpx;" class="Skeleton" />
							<view style="height: 30rpx;" class="Skeleton" />
						</view>
					</view>
				</template>
				<view class="add" @click="jumpAdd">
					<u-icon color='#fff' name='man-add' size='50rpx' />
				</view>
			</z-paging>
		</view>
		<root-portal>
			<userPopup :show='userShow' @close='userShow=false' @slecetUser='slecetUser'/>
			<u-action-sheet :actions="columns" :show="selectShow" @close='selectShow=false' @select="confirm"
				closeOnClickOverlay></u-action-sheet>
			<u-modal :show="delShow" content='是否确认删除？' showCancelButton @confirm='del(selectItem.id)'
				@cancel='delShow=false'></u-modal>
		</root-portal>
	</view>
</template>

<script>
	import filter from '@/components_company/filter.vue'
	import userPopup from './components/userPopup.vue'
	export default {
		components: {
			filter,
			userPopup
		},
		data() {
			return {
				delShow: false,
				data: [],
				loading: true,
				disabled: false,
				selectItem: {},
				selectShow: false,
				keywords: null,
				userShow: false
			}
		},
		computed: {
			columns() {
				return [{
						name: '流程'
					},
					{
						name: '动态'
					},
					{
						name: this.selectItem.is_show ? '隐藏' : '显示'
					},
					{
						name: this.selectItem.is_recommend ? '取消推荐' : '推荐'
					},
					{
						name: this.selectItem.is_finished ? '未完工' : '完工'
					},
					{
						name: '摄像头'
					}, {
						name: '更改'
					}, {
						name: '编辑'
					},
					{
						name: '删除'
					},
				]
			}
		},
		methods: {
			slecetUser(e){
				this.loading=true
				this.$api_company.Decorate.Put(this.selectItem.id,e.user_id).then(res=>{
					uni.$u.toast('更换成功')
					this.$refs.paging.reload()
				}).catch(err=>{
					this.loading=false
				})
			},
			reload(e) {
				this.keywords = e.keywords
				this.$refs.paging.reload()
			},
			confirm(e) {
				this.selectShow = false
				if (e.name == '编辑') {
					this.update(this.selectItem.id)
				}  else if(e.name == '更改') {
					this.userShow=true
				} else if (e.name == '动态') {
					this.$utils.navigateTo({
						url: '/active/add?decorate_id=' + this.selectItem.id
					})
				} else if (e.name == '删除') {
					this.delShow = true
				} else if (e.name == '流程') {
					this.$utils.navigateTo({
						url: '/process/customerProcess?id=' + this.selectItem.id
					})
				} else if (e.name == '摄像头') {
					this.$utils.navigateTo({
						url: '/customer_company/camera?id=' + this.selectItem.id
					})
				} else if (e.name == '隐藏') {
					this.data = this.data.map(item => {
						if (item.id != this.selectItem.id) {
							return item
						}
						return {
							...item,
							is_show: 0
						}
					})
					this.$api_company.Decorate.Change(this.selectItem.id, 'show')
				} else if (e.name == '显示') {
					this.data = this.data.map(item => {
						if (item.id != this.selectItem.id) {
							return item
						}
						return {
							...item,
							is_show: 1
						}
					})
					this.$api_company.Decorate.Change(this.selectItem.id, 'show')
				} else if (e.name == '取消推荐') {
					this.data = this.data.map(item => {
						if (item.id != this.selectItem.id) {
							return item
						}
						return {
							...item,
							is_recommend: 0
						}
					})
					this.$api_company.Decorate.Change(this.selectItem.id, 'recommend')
				} else if (e.name == '推荐') {
					this.data = this.data.map(item => {
						if (item.id != this.selectItem.id) {
							return item
						}
						return {
							...item,
							is_recommend: 1
						}
					})
					this.$api_company.Decorate.Change(this.selectItem.id, 'recommend')
				} else if (e.name == '未完工') {
					this.data = this.data.map(item => {
						if (item.id != this.selectItem.id) {
							return item
						}
						return {
							...item,
							is_finished: 0
						}
					})
					this.$api_company.Decorate.Change(this.selectItem.id, 'finish')
				} else if (e.name == '完工') {
					this.data = this.data.map(item => {
						if (item.id != this.selectItem.id) {
							return item
						}
						return {
							...item,
							is_finished: 1
						}
					})
					this.$api_company.Decorate.Change(this.selectItem.id, 'finish')
				}
			},
			query(page, size) {
				let obj = {
					keywords: this.keywords,
					page,
					size
				}
				uni.showLoading({
					title: '加载中...'
				})
				this.$api_company.Decorate.List(obj).then(res => {
					this.$refs.paging.complete(res.data.customers.data.map(item => {
						return {
							...item,
							avatar: item.customer?.avatar
						}
					}))
					this.loading = false
					uni.hideLoading()
				}).catch(err => {
					console.log(err)
					uni.hideLoading()
				})
			},
			openSelect(item) {
				this.selectShow = true
				this.selectItem = item
			},
			jumpAdd() {
				this.$utils.navigateTo({
					url: '/customer_company/add'
				})
			},
			jumpDetail(id, avatar) {
				this.$utils.navigateTo({
					url: '/customer_company/detail?id=' + id + '&avatar=' + avatar
				})
			},
			update(id) {

				this.$utils.navigateTo({
					url: '/customer_company/upDate?id=' + id
				})
			},
			del(id) {
				this.delShow = false
				if (this.disabled) return
				this.disabled = true
				uni.showLoading({
					title: '删除中...'
				})
				this.$api_company.Decorate.Del(id).then(err => {
					this.data = this.data.filter(item => item.id != id)
					this.disabled = false
					uni.hideLoading()
				}).catch(err => {
					uni.hideLoading()
					uni.$u.toast(err.message)
					this.disabled = false
				})
			}
		}
	}
</script>

<style lang="scss">
	.iconfont {
		color: red;
		position: absolute;
		z-index: 11;
	}

	.page {
		padding: 0rpx 20rpx;
		height: 100vh;
		padding-bottom: calc(constant(safe-area-inset-bottom) + 10rpx);
		/* 兼容 iOS 设备 */
		padding-bottom: calc(env(safe-area-inset-bottom) + 10rpx);
		box-sizing: border-box;
		display: flex;
		flex-direction: column;

		.userInfoBox {
			display: flex;
			margin-bottom: 20rpx;

			>view:nth-child(2) {
				margin-left: 10px;
				flex: 1;
				display: flex;
				height: 100rpx;
				flex-direction: column;
				justify-content: space-between;
				border-bottom: 1rpx solid #f1f1f1;
				padding: 0 0 10rpx 0;
			}
		}

		.add {
			position: fixed;
			right: 5vw;
			bottom: 20vh;
			border-radius: 100%;
			box-shadow: 0 0 10rpx #ddd;
			background: rgba(0, 0, 0, 0.7);
			padding: 20rpx;

		}
	}

	.flex {
		display: flex;

	}
</style>